﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TCDL.BUS;
using TCDL.GLOBAL;

namespace TCDL
{
    public partial class frmQLyLichTrinh : Form
    {
        XemDSBUS ds = new XemDSBUS();
        int iCount = 1;
        public frmQLyLichTrinh()
        {
            InitializeComponent();
            cbxGaDen.DataSource = ds.XemDS("sp_XemDSGa", null, 0);
            cbxGaDen.DisplayMember = "TenGa";
            cbxGaDen.ValueMember = "MaGa";

            cbxGaDi.DataSource = ds.XemDS("sp_XemDSGa", null, 0);
            cbxGaDi.DisplayMember = "TenGa";
            cbxGaDi.ValueMember = "Maga";
        }

        private void frmQLyLichTrinh_Load(object sender, EventArgs e)
        {
            dgvLichVanChuyen.DataSource = ds.XemDS("sp_XemDSLichTrinh", null, 0);
        }
        
        int SelectedRowIndex = -1;
        private void dgvLichVanChuyen_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvLichVanChuyen.SelectedCells.Count > 0)
            {
                SelectedRowIndex = dgvLichVanChuyen.SelectedCells[0].RowIndex;
            }

            int iMaGaDi = Int32.Parse(dgvLichVanChuyen.Rows[SelectedRowIndex].Cells["GaDi"].Value.ToString());
            int iMaGaDen = Int32.Parse(dgvLichVanChuyen.Rows[SelectedRowIndex].Cells["GaDen"].Value.ToString());
            
            cbxGaDen.SelectedValue = iMaGaDen;
            cbxGaDi.SelectedValue = iMaGaDi;
            txtLichTrinh.Text = dgvLichVanChuyen.Rows[SelectedRowIndex].Cells["TenLT"].Value.ToString();
            txtDonGia.Text = dgvLichVanChuyen.Rows[SelectedRowIndex].Cells["DonGia"].Value.ToString();
            txtSoNgay.Text = dgvLichVanChuyen.Rows[SelectedRowIndex].Cells["SoNgay"].Value.ToString();
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            int iMaLT = Int32.Parse(dgvLichVanChuyen.Rows[SelectedRowIndex].Cells["MaLT"].Value.ToString());
            string strDonGia = txtDonGia.Text;
            string[] temp = strDonGia.Split('.');
            int iDonGia = Int32.Parse(temp[0]);
            
            iCount = 2;
            gConnect[] gCon = new gConnect[iCount];
            gCon[0] = new gConnect("@MaLT", iMaLT);
            gCon[1] = new gConnect("@DonGia", iDonGia);
            ds.ThemDS("Sp_CapNhatGiaLichTrinh", gCon, iCount);
            frmQLyLichTrinh_Load(sender, e);
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
